//
//  ViewController.m
//  SchoolProduct
//
//  Created by feel on 16/8/31.
//  Copyright © 2016年 sm. All rights reserved.
//

#import "ViewController.h"
#import "UIKit+AFNetworking.h"
#import "AFNetworking.h"

#import "RegistViewController.h"
#import "Regist2ViewController.h"

#import "HomeViewController.h"
#import "NoticeViewController.h"
#import "SchoolViewController.h"
#import "MyViewController.h"

#import "LoginModel.h"
@interface ViewController ()
{
    UIImageView *View;
    UIView *bgView;
    UITextField *pwd;
    UITextField *user;
    UIButton *QQBtn;
    UIButton *weixinBtn;
    UIButton *xinlangBtn;
}
@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSDictionary *urlParameters;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"nihao");
    
//    _urlStr = @"http://www.100xsys.com.cn/school/app/commonAccoutInfo/login";
//    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"xuesheng1",@"accoutInfo.loginName",@"123456789",@"accoutInfo.password", nil];
////    [_urlParameters setObject:@"xuesheng1" forKey:@"accoutInfo.loginName"];
////    [_urlParameters setObject:@"123456" forKey:@"accoutInfo.password"];
//    [self fecthDataFromNet:_urlStr parameters:_urlParameters];
    
    _urlStr = @"http://www.100xsys.com.cn/school/app/commonAccoutInfo/login";
    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"xuesheng1",@"accoutInfo.loginName",@"123456789",@"accoutInfo.password", nil];
    [self fecthDataFromNet:_urlStr parameters:_urlParameters];

    
    //引导页图片
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"welcome.png"]];
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:imageView];
    //动画
    [UIView animateWithDuration:2 animations:^{
        imageView.alpha = 0.9;
        
    } completion:^(BOOL finished) {
        
        [imageView removeFromSuperview];
        
        self.title = @"登录";
        
        View=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        View.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:View];
        
        //创建textfield
        [self createTextField];
        //创建登录 注册 按钮
        [self createButtons];
    }];
}

//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"登录-%@",dic[@"message"]);
        NSLog(@"登录-%@",dic);
        LoginModel * loginM = [[LoginModel alloc]init];
        [loginM setValuesForKeysWithDictionary:dic[@"info"]];
        
        NSLog(@"%@",loginM.webConfig.phone);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        

    }];
}

//创建textfield
-(void)createTextField
{
    CGRect frame=[UIScreen mainScreen].bounds;
    bgView=[[UIView alloc]initWithFrame:CGRectMake(10, 75, frame.size.width-20, 100)];
    bgView.layer.cornerRadius=3.0;
    bgView.alpha=0.7;
    bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgView];
    
    user = [[UITextField alloc]initWithFrame:CGRectMake(60, 10, 271, 30)];
    user.font = [UIFont systemFontOfSize:14];
    user.placeholder = @"手机号/身份证号码";
//    user.keyboardType=UIKeyboardTypeNumberPad;
    user.clearButtonMode = UITextFieldViewModeWhileEditing;
    [bgView addSubview:user];
    
    pwd = [[UITextField alloc]initWithFrame:CGRectMake(60, 60, 271, 30)];
    pwd.font = [UIFont systemFontOfSize:14];
    pwd.placeholder = @"请输入您的密码";
    pwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    //密文样式
    pwd.secureTextEntry=YES;
    //pwd.keyboardType=UIKeyboardTypeNumberPad;
    
    [bgView addSubview:pwd];
    
}
//创建登录 注册 按钮
-(void)createButtons
{
    //登录
    UIButton * landBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    landBtn.frame = CGRectMake(10, 190, self.view.frame.size.width-20, 37);
    [landBtn setTitle:@"登录" forState:UIControlStateNormal];
    [landBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    landBtn.titleLabel.font = [UIFont systemFontOfSize:19];
    landBtn.backgroundColor=[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1];
    [landBtn addTarget:self action:@selector(landClick) forControlEvents:UIControlEventTouchUpInside];
    landBtn.layer.cornerRadius=5.0f;
    [self.view addSubview:landBtn];
    
    //注册
    UIButton * RegistBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    RegistBtn.frame = CGRectMake(5, 235, 70, 30);
    [RegistBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [RegistBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    RegistBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [RegistBtn addTarget:self action:@selector(registration:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:RegistBtn];
    
    //找回密码
    UIButton * LookForBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    LookForBtn.frame = CGRectMake(self.view.frame.size.width-75, 235, 60, 30);
    [LookForBtn setTitle:@"记住密码" forState:UIControlStateNormal];
    [LookForBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    LookForBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    //    [LookForBtn addTarget:self action:@selector(fogetPwd:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:LookForBtn];
    
}

//登录的方法
-(void)landClick
{
//    if (user.text.length <= 0 || pwd.text.length <= 0)
//    {
//        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"手机号或密码不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//    }
//    else
//    {
//        NSString * name = [APP_DEF objectForKey:@"userPhone"];
//        NSString * password = [APP_DEF objectForKey:@"userpassward"];
//        
//        if (user.text == name || pwd.text == password)
//        {
//            NSLog(@"登录成功");
//            [APP_DEF setObject:user.text forKey:@"userPhone"];
//            [APP_DEF setObject:pwd.text forKey:@"userpassward"];
//            [APP_DEF synchronize];
//            [self goToMainVC];
//        }
        //        else
        //        {
        //            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"手机号或密码不正确" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        //            [alert show];
        //        }
        
//        NSString *urlStr = [NSString stringWithFormat:@"http://www.100xsys.com.cn/school/app/commonAccoutInfo/login?accoutInfo.loginName=%@&accoutInfo.password=%@",user.text,pwd.text];
//        NSURL *url = [NSURL URLWithString:urlStr];
//        NSURLRequest *request = [NSURLRequest requestWithURL:url];
//        NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
//        [connection start];
        
    
        
//        LoginModel * loginM = [[LoginModel alloc]init];
        
//        NSString * name = [APP_DEF objectForKey:@"accoutInfo.loginName"];
//        NSString * password = [APP_DEF objectForKey:@"accoutInfo.password"];
//        NSLog(@"+++++%d",loginM.success);
//        if (loginM.success == 1)
//        {
////            [APP_DEF setObject:user.text forKey:@"accoutInfo.loginName"];
////            [APP_DEF setObject:pwd.text forKey:@"accoutInfo.password"];
////            [APP_DEF synchronize];
            [self goToMainVC];
//        }
//    }
    
    
}



//忘记密码方法
-(void)registration:(UIButton *)btn
{
    //进入注册页面
    Regist2ViewController * registVC = [[Regist2ViewController alloc]init];
    [self.navigationController pushViewController:registVC animated:YES];
}

//四个界面
-(void)goToMainVC
{
    HomeViewController * messageVC = [[HomeViewController alloc]init];
    messageVC.title = @"首页";
    messageVC.tabBarItem.image = [UIImage imageNamed:@"导航-01.png"];
    messageVC.tabBarItem.selectedImage = [UIImage imageNamed:@"导航-02.png"];
    UINavigationController * navMessage = [[UINavigationController alloc]initWithRootViewController:messageVC];
    
    NoticeViewController * schoolVC = [[NoticeViewController alloc]init];
    schoolVC.title = @"通知";
    schoolVC.tabBarItem.image = [UIImage imageNamed:@"信息-01.png"];
    schoolVC.tabBarItem.selectedImage = [UIImage imageNamed:@"信息-02.png"];
    UINavigationController * navSchool = [[UINavigationController alloc]initWithRootViewController:schoolVC];
    
    SchoolViewController * echatVC = [[SchoolViewController alloc]init];
    echatVC.title = @"教学圈";
    echatVC.tabBarItem.image = [UIImage imageNamed:@"导航-03.png"];
    echatVC.tabBarItem.selectedImage = [UIImage imageNamed:@"导航-04.png"];
    UINavigationController * navEchat = [[UINavigationController alloc]initWithRootViewController:echatVC];
    
    
    MyViewController * myVC = [[MyViewController alloc]init];
    myVC.title = @"我的";
    myVC.tabBarItem.image = [UIImage imageNamed:@"daohang05.png"];
    myVC.tabBarItem.selectedImage = [UIImage imageNamed:@"导航-06.png"];
    UINavigationController * navMy = [[UINavigationController alloc]initWithRootViewController:myVC];
    
    
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    tabBar.viewControllers = @[navMessage,navSchool,navEchat,navMy];
    tabBar.tabBar.tintColor = [UIColor orangeColor];
    APP_WIN.rootViewController = tabBar;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
